<?PHP 
defined('_ACCESS') or die( 'Direct Access to this location is not allowed.' );
defined('_LOCATION') or die( 'Direct Access to this location is not allowed.' );

global $db, $item, $id, $option;
//global $start, $inpage, $filter_author;
//global $Prod_name, $Filial_name;

if (!$db->getSetting('Prod_id')) exit();
switch ($db->getSetting('access'))
{
	case 'user':	
				
		$Prod_id = $db->getSetting('Prod_id');	
		$Lang = $db->getSetting('Lang');
		$User_id = $db->getSetting('User_id');	
		
		$res=$db->query("SELECT $Lang AS trans
					FROM Translation INNER JOIN ProdList
											ON Translation.id = ProdList.translation_link
					WHERE     (ProdList.id = $Prod_id) LIMIT 0, 1");
		$cur_prod = $db->fetchObject($res);
		$Prod_name = $cur_prod->trans;
		
		
		//массив параметров
		$params_names = array();
		
		//массив параметров
		$params = array();
		//массив типов
		$types = array();
		//массив фиксированных значений параметров
		$param_values = array();	
		//признак начала новой таблицы
		$mustNewTable = false;		
		//========================================================================
		$res=$db->query("SELECT $Lang AS trans, ParamList.has_fixed_values,  ProdAndParam.prod_link, ProdAndParam.param_link, ParamList.name, ProdAndParam.order
						FROM Translation INNER JOIN  ParamList 
												ON Translation.id = ParamList.translation_link 
										INNER JOIN ProdAndParam 
												ON ParamList.id = ProdAndParam.param_link
						WHERE     (ProdAndParam.prod_link = $Prod_id) 
						ORDER BY ProdAndParam.order");
		while($cur_param = $db->fetchObject($res))
		{	
			$params[$cur_param->param_link] = $cur_param;		
			$params_names[$cur_param->param_link] = $cur_param->trans;
		}	
		//========================================================================
		$res=$db->query("SELECT $Lang AS trans, TypeList.id
						FROM         Translation INNER JOIN
	                	    	  		TypeList INNER JOIN
	                    	  			ProdAndType ON TypeList.id = ProdAndType.prodtype_link ON Translation.id = TypeList.translation_link
					WHERE     (ProdAndType.prod_link = $Prod_id)");	
		
		while($cur_type = $db->fetchObject($res))
		{	
			$types[$cur_type->id] = $cur_type;		
		}	
		$old_table_num = 0;
		$lines = array();
		
		$par_keys = array_keys($params);
		$i=0;
		foreach($params as $param_key => $param_value)
		{
			$p_key=$par_keys[$i++];
			$cur_ord = (int) (floor(($param_value->order) / 100) - 1);
			$lines[$cur_ord][$p_key] = array_shift($params);
		}
	?>
	
	
	
		<div id="main">		
		<div class="prod_name"><?php echo $Prod_name;?></div>
		<form method="post" action="index.php" name="loginForm" id="loginForm" enctype="multipart/form-data">		
		
		<div class="saveButton">		
			<input type="submit" class="inputbox" value="<?php echo WWW_SAVE;?>" />
			<input type="hidden" name="post" value="1" />
	 		<input type="hidden" name="option" value="new" />
	 		<input type="hidden" name="item" value="prod" />
		</div> 	
	<?php	
			
		foreach($lines as $line_value)
		{
	?>
		<div class="container">
			<table cellspacing="0" cellpadding="0" class="data">		
				<tr>
					<td><?php echo WWW_TYPE;?></td>						 
	<?php					
			foreach($line_value as $param_key => $param_value)
			{		
	?>				
					<td class="text"><?php echo $param_value->trans;?><?php $param_key;?></td>
	<?php 
			}
	?>			</tr>
	<?php	
		foreach($types as $type_key => $type_value)
		{	
	?>				
				<tr>
					<td class="text"><?php echo $type_value->trans;?></td>					
	<?php
			foreach($line_value as $param_key => $param_value)
			{					
				$cur_val = null;
				$res=$db->query("SELECT Data.val, Data.id
								FROM TypeList INNER JOIN
										Data INNER JOIN
				                      ProdList ON Data.prod_link = ProdList.id INNER JOIN
				                      ParamList ON Data.param_link = ParamList.id ON TypeList.id = Data.prod_type_link 
					WHERE     (Data.user_link = $User_id) 
								AND (Data.prod_link = $Prod_id) 
								AND (Data.prod_type_link = $type_key) 
								AND (Data.modif_date like '".flipDate($db->getSetting('ViewDate'), '.')."%') 
								AND (Data.param_link = $param_key) LIMIT 1");
				
				if ($cur_val = $db->fetchObject($res)) 
				{		
					$curv_val = $cur_val->val;
					$curv_id = $cur_val->id;
				}
				else 
				{
					$curv_val = '';
					$curv_id = '-1';
				}
				?>
				<td class="text">
				<?php  
				if ($param_value->has_fixed_values == '1')
				{				
					$param_values = array();
	
					$res=$db->query("SELECT $Lang AS trans, paramandvalues.param_link, paramandvalues.value_link, paramvalueslist.name, paramandvalues.order
						FROM Translation, paramvalueslist, paramandvalues
						WHERE (paramandvalues.param_link = $param_key) and
								(Translation.id = paramvalueslist.translation_link) and
								(paramvalueslist.id = paramandvalues.value_link)
						ORDER BY paramandvalues.order");
					while($cur_param_value = $db->fetchObject($res))
					{				
						$param_values[$cur_param_value->value_link] = $cur_param_value->trans;					
					}				
					PrintHTML::listOut("data[$type_key][$param_key]", $param_values, $curv_val);
				}
				elseif ($param_value->has_fixed_values == '0')
				{			
					PrintHTML::editOut("data[$type_key][$param_key]", $curv_val, 5, "input_data");
				}
				?>
				</td>
				<?php
				PrintHTML::hiddenOut("data_id[$type_key][$param_key]", $curv_id);			
			}
				?>			
			</tr>
				<?php
		}
	?>
			</table>
		</div>
		
	<?php		
		}		
	?>			
						
				<div class="saveButton">
					<input type="submit" class="inputbox" value="<?php echo WWW_SAVE;?>" />
				</div>
				
				</form>		
<?php	
		
		break;
//=======================================================================================================================
//=======================================================================================================================
//=======================================================================================================================
	case 'boss':		
		$Prod_id = $db->getSetting('Prod_id');	
		$Lang = $db->getSetting('Lang');
		$User_id = $db->getSetting('User_id');	
		
		
		
		
		$res=$db->query("SELECT $Lang AS trans
					FROM Translation INNER JOIN ProdList
											ON Translation.id = ProdList.translation_link
					WHERE     (ProdList.id = $Prod_id) LIMIT 0, 1");
		$cur_prod = $db->fetchObject($res);
		$Prod_name = $cur_prod->trans;
		
		
		//массив параметров
		$params_names = array();
		
		//массив параметров
		$params = array();
		//массив типов
		$types = array();
		//массив фиксированных значений параметров
		$param_values = array();	
		//признак начала новой таблицы
		$mustNewTable = false;		
		//========================================================================
		$res=$db->query("SELECT $Lang AS trans, ParamList.has_fixed_values,  ProdAndParam.prod_link, ProdAndParam.param_link, ParamList.name, ProdAndParam.order
						FROM Translation INNER JOIN  ParamList 
												ON Translation.id = ParamList.translation_link 
										INNER JOIN ProdAndParam 
												ON ParamList.id = ProdAndParam.param_link
						WHERE     (ProdAndParam.prod_link = $Prod_id) 
						ORDER BY ProdAndParam.order");
		while($cur_param = $db->fetchObject($res))
		{	
			$params[$cur_param->param_link] = $cur_param;		
			$params_names[$cur_param->param_link] = $cur_param->trans;
		}	
		//========================================================================
		$res=$db->query("SELECT $Lang AS trans, TypeList.id
						FROM         Translation INNER JOIN
	                	    	  		TypeList INNER JOIN
	                    	  			ProdAndType ON TypeList.id = ProdAndType.prodtype_link ON Translation.id = TypeList.translation_link
					WHERE     (ProdAndType.prod_link = $Prod_id)");	
		
		while($cur_type = $db->fetchObject($res))
		{	
			$types[$cur_type->id] = $cur_type;		
		}	
		$old_table_num = 0;
		$lines = array();
		
		$par_keys = array_keys($params);
		$i=0;
		foreach($params as $param_key => $param_value)
		{
			$p_key=$par_keys[$i++];
			$cur_ord = (int) (floor(($param_value->order) / 100) - 1);
			$lines[$cur_ord][$p_key] = array_shift($params);
		}
	?>	
		
	
		<div id="main">		
		<div class="prod_name"><?php echo $Prod_name;?></div>
		
		<form method="post" action="index.php" name="loginForm" id="loginForm" enctype="multipart/form-data">
		
		
		<div class="saveButton">	
			<input type="hidden" name="post" value="0" />
		 	<input type="hidden" name="option" value="new" />    
		 	<input type="hidden" name="item" value="graph" />			
		</div>	 	
	 	
	<?php	
		$old_table_num = 0;
		$lines = array();
		
		$par_keys = array_keys($params);
		$i=0;
		foreach($params as $param_key => $param_value)
		{
			$p_key=$par_keys[$i++];
			$cur_ord = (int) (floor(($param_value->order) / 100) - 1);
			$lines[$cur_ord][$p_key] = array_shift($params);
		}
		//print_r($lines);
		
		
		
		foreach($lines as $line_value)
		{
	?>
		<div class="container">
			<table cellspacing="0" cellpadding="0" class="data">		
				<tr>
					<td><?php echo WWW_TYPE;?></td>						 
	<?php					
			foreach($line_value as $param_key => $param_value)
			{		
	?>				
					<td class="text"><?php echo $param_value->trans;?><?php $param_key;?></td>
	<?php 
			}
	?>			</tr>
	<?php	
		foreach($types as $type_key => $type_value)
		{	
	?>				
					<tr>
						<td class="text"><?php echo $type_value->trans;?></td>					
	<?php
			foreach($line_value as $param_key => $param_value)
			{					
				$cur_val = null;
				$res=$db->query("SELECT Data.val, Data.id
								FROM TypeList INNER JOIN
										Data INNER JOIN
				                      ProdList ON Data.prod_link = ProdList.id INNER JOIN
				                      ParamList ON Data.param_link = ParamList.id ON TypeList.id = Data.prod_type_link 
					WHERE     (Data.user_link = $User_id) 
								AND (Data.prod_link = $Prod_id) 
								AND (Data.prod_type_link = $type_key) 
								AND (Data.modif_date like '".flipDate($db->getSetting('ViewDate'), '.')."%') 
								AND (Data.param_link = $param_key) LIMIT 1");
				
				if ($cur_val = $db->fetchObject($res)) 
				{		
					$curv_val = $cur_val->val;
					$curv_id = $cur_val->id;
				}
				else 
				{
					$curv_val = '';
					$curv_id = '-1';
				}
				?>
				<td class="text">
				<?php  
				if ($param_value->has_fixed_values == '1')
				{				
					$param_values = array();
	
					$res=$db->query("SELECT $Lang AS trans, paramandvalues.param_link, paramandvalues.value_link, paramvalueslist.name, paramandvalues.order
						FROM Translation, paramvalueslist, paramandvalues
						WHERE (paramandvalues.param_link = $param_key) and
								(Translation.id = paramvalueslist.translation_link) and
								(paramvalueslist.id = paramandvalues.value_link)
						ORDER BY paramandvalues.order");
					while($cur_param_value = $db->fetchObject($res))
					{				
						$param_values[$cur_param_value->value_link] = $cur_param_value->trans;					
					}				
					PrintHTML::listOut("data[$type_key][$param_key]", $param_values, $curv_val);
				}
				elseif ($param_value->has_fixed_values == '0')
				{			
					PrintHTML::editOut("data[$type_key][$param_key]", $curv_val, 5, "input_data");
				}
				?>
				</td>
				<?php
				PrintHTML::hiddenOut("data_id[$type_key][$param_key]", $curv_id);			
			}
				?>			
			</tr>
				<?php
		}
	?>
			</table>
		</div>		
		
		
	<?php		
			
			
			
		}
		
	?>
				
						
				<div class="saveButton">
				<?php 
					PrintHTML::listOut("param_graph", $params_names, 0);
					?>				
					
					<input type="submit" class="inputbox" value="<?php echo WWW_BUILD;?>" />
					<p>
						<img src="mygif2.gif"/>
					</p>				
				</div>		
				
				</form>		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	<?php
		
		break;
	case 'admin':
		echo 'Youre ADMIN. Go to Admin Panel';
		
		break;

	default:
		break;
}





?>



				

</div>
<?php
$db->Deb_PRNT();
?>				